package com.cv.system.controller.audit.auth;

import com.cv.framework.common.pojo.Result;
import com.cv.framework.security.config.SecurityProperties;
import com.cv.framework.security.core.utils.SecurityFrameworkUtil;
import com.cv.system.controller.audit.auth.vo.AuditAuthLoginReqVO;
import com.cv.system.controller.audit.auth.vo.AuditAuthLoginRespVO;
import com.cv.system.controller.audit.auth.vo.AuditUserLoginProfileRespVO;
import com.cv.system.service.auth.AuditAuthService;
import com.cv.system.service.user.SystemUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import static com.cv.framework.common.pojo.Result.success;

/**
 * 客服审核平台 - 登录认证
 *
 * @author Charles_XDXD
 */
@Tag(name = "客服审核平台 - 登录认证")
@RestController
@RequestMapping("/system/auth")
public class AuditAuthLoginController {

    @Resource
    private AuditAuthService auditAuthService;

    @Resource
    private SystemUserService systemUserService;

    @Resource
    private SecurityProperties securityProperties;

    @PostMapping("/login")
    @Operation(summary = "帐号密码登录")
    @PermitAll
    public Result<AuditAuthLoginRespVO> login(@RequestBody @Valid AuditAuthLoginReqVO auditAuthLoginReqVO) {
        return success(auditAuthService.login(auditAuthLoginReqVO));
    }

    @PostMapping("/logout")
    @Operation(summary = "登出系统")
    @PermitAll
    public Result<Boolean> logout(HttpServletRequest request) {
        SecurityFrameworkUtil.obtainAuthorization(request,
                securityProperties.getTokenHeader(), securityProperties.getTokenHeader());
        return success(true);
    }

    @GetMapping("/user/profile")
    @Operation(summary = "获得登录用户基本信息")
    public Result<AuditUserLoginProfileRespVO> getLoginUserProfile() {
        return success(systemUserService.getUserAuditProfile(SecurityFrameworkUtil.getLoginUserId()));
    }

}
